
---@class cwh_DIYUtility
local DIY = require "packages.sanshe.pkg.utility._base"

---------------------------------------------------------------------------------------------------

--[[  修改自 diy_utility
  新系统 蓄势技

  蓄势技使用后进入蓄势中状态，不可使用。
  有蓄势技的角色在出牌阶段空闲时间点，可以弃置三张牌，复原一个本轮未使用的蓄势技。
  洗牌后，所有角色复原已使用的蓄势技。

  标记格式 {技能名，蓄势进度，蓄势上限，复原范围}
  复原范围影响的是蓄势按钮，即 “本轮未使用” 可改为 “本阶段/回合”
--]]

--- 蓄势技标记（放在技能骨架skeleton的tags中）
DIY.ReadySkill = "cwh_ready_skill"

Fk:loadTranslationTable{
  ["cwh_diy_ready_skill"] = "一种技能标签，拥有此标签的技能发动后失效，直到牌堆洗切后重置；出牌阶段，你可以弃置X张牌，"..
  "重置一个本回合未发动过的蓄势技（X为此技能蓄势上限减蓄势进度，蓄势上限默认为3）。",
  ["@cwh_diy_skill_readying"] = "蓄势",
}

-- 角色的某技能是否蓄势中
---@param player Player @ 目标角色
---@param skill Skill|string @ 目标技能
DIY.isReadying = function(player, skill)
  if type(skill) == "string" then
    skill = Fk.skills[skill]
    if skill == nil then return false end
  end
  if not skill:getSkeleton() then return false end
  local skill_name = skill:getSkeleton().name
  local ready = player:getTableMark("cwh_diy_skill_ready")
  for _, mark in ipairs(ready) do
    if mark[1] == skill_name then
      return mark[3] > mark[2]
    end
  end
  return false
end

-- 变更角色某技能蓄势进度/上限/范围，也可用于初始化蓄势技
---@param player ServerPlayer @ 目标角色
---@param skill Skill|string @ 目标技能
---@param num integer? @当前蓄势进度，不超过上限
---@param max integer? @蓄势上限，进度与上限默认为3
---@param scope integer? @复原范围，round/turn/phase，默认为turn
DIY.setReady = function(player, skill, num, max, scope)
  if type(skill) == "string" then
    skill = Fk.skills[skill]
    if skill == nil then return end
  end
  if not skill:getSkeleton() then return end
  local skill_name = skill:getSkeleton().name
  local ready = player:getTableMark("cwh_diy_skill_ready")
  if not table.find(ready,function (m)
    return m[1] == skill_name
  end) then
    num = num or 3
    max = max or 3
    scope = scope or Player.HistoryTurn
    player.room:addTableMark(player,"cwh_diy_skill_ready",{skill_name, num, max, scope})
    table.insert(ready,{skill_name, num, max, scope})
  end
  local readying = {}
  for i, mark in ipairs(ready) do
    if mark[1] == skill_name then
      if num then mark[2] = math.max(0, num) end
      if max then mark[3] = math.max(0, max) end
      if scope and table.contains({Player.HistoryRound,Player.HistoryTurn,Player.HistoryPhase},scope) then mark[4] = scope end
      mark[2] = math.min(mark[2],mark[3])
      ready[i] = mark
    end
    if mark[2] ~= mark[3] then
      table.insert(readying,Fk:translate(skill_name).." "..mark[2].."/".. mark[3])
    end
  end
  player.room:setPlayerMark(player,"cwh_diy_skill_ready",ready)
  player.room:setPlayerMark(player,"@cwh_diy_skill_readying",#readying == 0 and 0 or readying )
end

-- 获取角色的某技能的当前蓄势进度/上限/复原范围，获取失败返回-1
---@param player Player @ 目标角色
---@param skill Skill|string @ 目标技能
---@param max boolean? @ 获取上限
---@param scope boolean? @获取复原范围
DIY.getReadyNum = function(player, skill, max, scope)
  if type(skill) == "string" then
    skill = Fk.skills[skill]
    if skill == nil then return -1 end
  end
  if not skill:getSkeleton() then return -1 end
  local skill_name = skill:getSkeleton().name
  local ready = player:getTableMark("cwh_diy_skill_ready")
  for _, mark in ipairs(ready) do
    if mark[1] == skill_name then
      return scope and mark[4] or ( max and mark[3] or mark[2])
    end
  end
  return -1
end

-- 复原角色的蓄势技
---@param player ServerPlayer @ 目标角色
---@param skill Skill|string @ 目标技能
DIY.removeReady = function(player, skill)
  DIY.setReady(player,skill,DIY.getReadyNum(player,skill,true))
end

-- 清空角色的蓄势技进度
---@param player ServerPlayer @ 目标角色
---@param skill Skill|string @ 目标技能
DIY.startReady = function(player, skill)
  DIY.setReady(player,skill,0)
end

-- 变更角色的某技能的当前蓄势进度/上限/复原范围
---@param player ServerPlayer @ 目标角色
---@param skill Skill|string @ 目标技能
---@param num integer? @变更数量，默认为1
---@param max boolean? @变更上限
---@param scope integer? @变更复原范围
DIY.changeReady = function(player, skill, num, max, scope)
  num = num or 1
  if max then
    DIY.setReady(player,skill,nil,DIY.getReadyNum(player,skill,true) + num)
  else
    DIY.setReady(player,skill,DIY.getReadyNum(player,skill) + num)
  end
  if scope then
    DIY.setReady(player,skill,nil,nil,scope)
  end
end



dofile 'packages/sanshe/pkg/utility/aux_events/ready_skill.lua'


return DIY
